package com.lifesting.tool.j2as3.action;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.search.ui.NewSearchUI;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

import com.lifesting.java2as3.analysis.OuterClassesQuery;

public class AnalysisAction implements IObjectActionDelegate {

	private IWorkbenchPart part;
	private IJavaProject project;
	public AnalysisAction() {
		
	}

	@Override
	public void setActivePart(IAction action, IWorkbenchPart targetPart) {
		this.part = targetPart;
	}

	@Override
	public void run(IAction action) {
		if (project == null) return;
		OuterClassesQuery query = new OuterClassesQuery(project);
		NewSearchUI.runQueryInBackground(query);
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		project = null;
		Object selected = ((StructuredSelection)selection).getFirstElement();
		if(selected instanceof IJavaProject){
			project = (IJavaProject) selected;
		}
		else if (selected instanceof IAdaptable){
			Object obj = ((IAdaptable)selected).getAdapter(IJavaElement.class);
			if (obj instanceof IJavaProject)
				project = (IJavaProject) obj;
		}

	}

}
